//
//  SevenGradientViewController.swift
//  swiftTS
//
//  Created by 航电 on 2020/4/16.
//  Copyright © 2020 航电. All rights reserved.
//

import UIKit

class SevenGradientViewController: UIViewController {

    fileprivate var _leftTimeLb:UILabel?;
    fileprivate var _rightTempertureLb:UILabel?;
    fileprivate var _lastPoint = CGPoint.zero;
    fileprivate var _currentTime:Int = 0;
    fileprivate var _currentTemperture:Int = 18;
    fileprivate var _gradientLayer:CAGradientLayer = {
        let gradient = CAGradientLayer();
        gradient.colors = [UIColor(red: 255.0/255.0, green: 218.0/255.0, blue: 185.0/255.0, alpha: 1.0).cgColor,UIColor(red: 135.0/255.0, green: 206.0/255.0, blue: 235.0/255.0, alpha: 1.0).cgColor];
        gradient.locations = [0.0,0.0];
        gradient.startPoint = CGPoint(x: 0.5, y: 1.0);
        gradient.endPoint = CGPoint(x: 0.5, y: 0.0);
        return gradient;
    }();
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "颜色渐变";
        self.view.backgroundColor = UIColor(red: 135.0/255.0, green: 206.0/255.0, blue: 235.0/255.0, alpha: 1.0);
        // Do any additional setup after loading the view.
        self.onCreateUIForView();
    }
    

    //MARK:UI
    fileprivate func onCreateUIForView() {
        self._gradientLayer.frame = self.view.bounds;
        self.view.layer.addSublayer(self._gradientLayer);
        
        _leftTimeLb = UILabel(frame: CGRect(x: Tool.kScreenWith(width: 10), y: Tool.HEIGHT_STATUS_NAVBAR+Tool.kScreenWith(width: 20), width: Tool.kScreenWith(width: 80), height: 20));
        _leftTimeLb?.backgroundColor = .clear;
        _leftTimeLb?.textAlignment = .center;
        _leftTimeLb?.textColor = .white;
        _leftTimeLb?.text = "\(_currentTime):00";
        self.view.addSubview(_leftTimeLb!);
        
        _rightTempertureLb = UILabel(frame: CGRect(x: screenMainSize.width-Tool.kScreenWith(width: 80)-Tool.kScreenWith(width: 10), y: Tool.HEIGHT_STATUS_NAVBAR+Tool.kScreenWith(width: 20), width: Tool.kScreenWith(width: 80), height: 20));
        _rightTempertureLb?.backgroundColor = .clear;
        _rightTempertureLb?.textAlignment = .center;
        _rightTempertureLb?.textColor = .white;
        _rightTempertureLb?.text = "\(_currentTemperture) 'C";
        self.view.addSubview(_rightTempertureLb!);
    }

    //MARK:Actions
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first;
        let currentPoint = touch?.location(in: self.view);
        
        if _lastPoint == .zero {
            _lastPoint = currentPoint!;
        } else {
            if _lastPoint.y > currentPoint!.y {
//                print("往上移");
                _currentTime += 1;
                
                if _currentTime > 23 {
                    _currentTime = 23;
                } else {
                    if _currentTime <= 12 {
                        _currentTemperture += 1;
                    } else {
                        _currentTemperture -= 1;
                        if _currentTemperture <= 18 {
                            _currentTemperture = 18;
                        }
                    }
                }
                _leftTimeLb?.text = "\(_currentTime):00";
                
                _rightTempertureLb?.text = "\(_currentTemperture) 'C";
                
            }
            else if _lastPoint.y <= currentPoint!.y {
//                print("往下移");
                
                _currentTime -= 1;
                if _currentTime < 0 {
                    _currentTime = 0;
                } else {
                    if _currentTime >= 12 {
                        _currentTemperture += 1;
                    } else {
                        _currentTemperture -= 1;
                    }
                }
                _leftTimeLb?.text = "\(_currentTime):00";
                
                _rightTempertureLb?.text = "\(_currentTemperture) 'C";
            }
            _lastPoint = currentPoint!;
            
            self.onChangeUIForView(time: _currentTime);
        }
    }
    
    //TODO:Animation
    fileprivate func onChangeUIForView(time:Int) {
        print("time:\(time)")
        if time <= 12 {
            let index = 0.2;
            let local = Float(index)*Float(time)*1.0/12.0;
            print("local:\(local)");
            self._gradientLayer.locations = [NSNumber(value: 0),NSNumber(value: local)];
        }
    }
}
